package com.tiantianyuegou.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tiantianyuegou.R;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/1/12.
 */
public class GoodsInfoAdapter extends BaseAdapter {

    private ArrayList<JSONArray> goodInfo;
    private Context context;
    private LayoutInflater inflater;

    public GoodsInfoAdapter() {

    }

    public GoodsInfoAdapter(Context context, ArrayList<JSONArray> goodInfo) {
        this.context = context;
        this.goodInfo = goodInfo;
        inflater = LayoutInflater.from(context);
        Log.e("tag","运行了goodsInfoAdapter");
    }

    @Override
    public int getCount() {
        return goodInfo.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        Log.e("tag","getView");
        Holder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_list_my_goods, null);
            holder = new Holder();
            holder.img_goods_picture = (ImageView) view.findViewById(R.id.img_goods_picture);
            holder.text_goods_name = (TextView) view.findViewById(R.id.text_goods_name);
            holder.text_goods_info = (TextView) view.findViewById(R.id.text_goods_info);
            holder.text_goods_money = (TextView) view.findViewById(R.id.text_goods_money);
            holder.text_goods_num = (TextView) view.findViewById(R.id.text_goods_num);
            holder.text_goods_state = (TextView) view.findViewById(R.id.text_goods_state);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        try {

            JSONArray array = new JSONArray();
            //JSONObject object=new JSONObject();
            Log.e("tag","getView");
            int num;
            for (int j = 0; j < goodInfo.size(); j++) {
                array = null;
                array = goodInfo.get(j);
                for (int as = 0; as < array.length(); as++) {
                    holder.text_goods_name.setText(array.getJSONObject(as).getString("name"));
                    holder.text_goods_info.setText(array.getJSONObject(as).getString("info"));
                    holder.text_goods_money.setText(array.getJSONObject(as).getString("money"));
                    holder.text_goods_num.setText(array.getJSONObject(as).getString("num"));
                    holder.text_goods_state.setText(array.getJSONObject(as).getString("state"));
                    ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
                    ImageLoader.getInstance().displayImage("http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=图%20动漫卡通&step_word=&pn=120&spn=0&di=145208230860&pi=&rn=1&tn=baiduimagedetail&is=&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=2328624548%2C705260753&os=3156803258%2C2373737972&simid=3392028471%2C210701974&adpicid=0&ln=1964&fr=ala&fmq=1452565888007_R&ic=undefined&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=&bdtype=0&objurl=http%3A%2F%2Fpicm.bbzhi.com%2Fyouxibizhi%2Fzhencangyouxidongmankatongkuan%2Fgame_gamewall_297823_m.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bkkzit_z%26e3Bv54AzdH3Fy57xtktzitAzdH3Fzijgvwg2y57xt15g24wghwp5g2h7wgAzdH3Fowssrwrj6_d8blma_z%26e3Bip4&gsm=b00003c", holder.img_goods_picture);

                    Log.e("tag","name:"+array.getJSONObject(as).getString("name"));

                    System.out.println(array.getJSONObject(as).getString("name"));
                    System.out.println(array.getJSONObject(as).getString("info"));
                    System.out.println(array.getJSONObject(as).getString("money"));
                    System.out.println(array.getJSONObject(as).getString("num"));
                    System.out.println(array.getJSONObject(as).getString("state"));
                    System.out.println(array.getJSONObject(as).getString("name"));
                }
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return view;
    }

    public static class Holder {
        TextView text_goods_name, text_goods_info, text_goods_money, text_goods_num, text_goods_state;
        ImageView img_goods_picture;
    }
}
